﻿using System;
using System.ComponentModel;
using BlueDot.Data.Core;
using BlueDot.Mobile.Core;
using CancelWorkFlowStep.Controllers;
using CancelWorkFlowStep.Model;

namespace CancelWorkFlowStep.Views
{
    public partial class UserView : ViewBase
    {
        #region Fields

        private const string UserViewShown = WellKnownActions.ViewShown + "/UserView";
        private const string UserViewShowning = WellKnownActions.ViewShowing + "/UserView";

        #endregion

        #region Constructors

        public UserView()
        {
            InitializeComponent();
        }

        [DependencyConstructor]
        public UserView(IMobileApplication application)
            : base(application)
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        [DependencyProperty(CreateNew = true)]
        public MainController MainController { get; set; }

        #endregion

        #region public

        /// <summary>
        /// This action receiver is called when UserView is shown.
        /// </summary>
        [Receiver(UserViewShown)]
        public void OnUserViewShown(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// This action receiver is called when UserView is showing.
        /// </summary>
        [Receiver(UserViewShowning)]
        public void OnUserViewShowning(object sender, EventArgs e)
        {
            // Replace the main menu on the shell with our view's menu
            ((Shell) Application.Shell).Menu = mainMenu;

            EntityDataView<User> users = MainController.GetUsers();
            var userList = (BindingList<User>) users.EntityList;

            User user = userList.AddNew();
            users.SelectedEntity = user;

            userBindingSource.DataSource = user;
        }

        public bool ValidateUser()
        {
            EntityDataView<User> users = MainController.GetUsers();
            return users.SelectedEntity.IsValid();
        }

        #endregion
    }
}